<?php
namespace Home\Controller;
use Think\Controller;
class CustomerServiceController extends CommnController{
    public function __construct(){
        parent::__construct();
    }

    /**
     *内部人员添加view
     */
    public function add_company_user(){
        //部门
        $apart=M('apart','','KEFU')->where(['status'=>1])->field('id,name')->select();
        //角色
       $role=M('role','','KEFU')->where(['status'=>1])->field('id,name')->select();
       $this->assign(array(
           'apart'=>$apart,
           'role'=>$role
       ));
        $this->display();
        die;
    }

    /**
     *内部人员列表
     */
    public  function  company_user_list(){

        $list=M('admin','','KEFU')->field('')->select();



    }


    /**
     *内部人员信息添加
     */
    public function add_company_user_save(){
        $name = I('name');
        $phone = I('phone');
        $pwd = I('pwd');
        $w_apart = I('w_apart');
        $w_rid=I('w_rid');
        $level=I('level');
        $email=I('email');
        $sex=I('sex');
        $pwd1 = set_pwd($pwd, $phone);
        $phone_e= M('admin', '', 'KEFU')->where(['phone'=>$phone])->select();
        if(!empty($phone_e)){
            $this->ajax('1002', '添加失败，手机号已经存在！');
            return false;
        }
        $str = array('name' => $name,
            'phone' => $phone,
            'pwd' => $pwd1,
            'status' => '1',
            'ctime' => time(),
            'role_id'=>$w_rid,
            'part_id'=>$w_apart,
            'sex'=>$sex,
            'email'=>$email,
            'level'=>$level,
            'out_kf_id'=>0);

        $add=M('admin','','KEFU')->add($str);
        if ($add) {
            $str=array('aid'=>$_SESSION['id'],'content'=>'新增账号'.$add,'ctime'=>time());
            M('admin_log','','KEFU')->add($str);
            $this->ajax('1001', '添加成功');
        } else {
            $this->ajax('1002', '添加失败');
        }


    }


    //添加ＩＰ
    public function add_ip(){
        if (empty($_POST)) {
            $this->display();
            die;
        }else{
            $ip=I('ip');
            $post_data = array(
                'ip' => $ip
            );
            $ret =$this->send_post('http://adm.jyyjrfw.com/External/external_add_allow_ip', $post_data);
            $json = json_decode($ret);
            $code=$json->code;
            if($code=='1001'){
                add_log('添加IP'.$ip);
            }
            $this->ajax($code, $json->result);

            //http://adm.jyyjrfw.com/External/external_add_allow_ip
            //http://adm.jyyjrfw.com/External/external_get_allow_ip  from

        }
    }

    public function Addkefu(){
        if (empty($_POST)) {
            $this->role=M('role', '', 'KEFU')->field('id,name')->where(['status'=>1,'id'=>['not in','1,3']])->select();
            $this->company=M('kf_out_company', '', 'KEFU')->field('id,kf_company_name as name')->where(['status'=>1])->select();
            $this->display();
        } else {
            $name = I('name');
            $phone = I('phone');
            $pwd = I('pwd');
            $company = I('company');
            $rid=I('rid');
            $phone_e= M('admin', '', 'KEFU')->where(['phone'=>$phone,'out_kf_id'=>$company])->select();
            if(!empty($phone_e)){
                $this->ajax('1002', '添加失败，手机号已经存在！');
                return false;
            }
            $pwd1 = set_pwd($pwd, $phone);
            $str = array('name' => $name, 'phone' => $phone, 'pwd' => $pwd1, 'status' => '1', 'ctime' => time(),'role_id'=>$rid,'out_kf_id'=>$company);
//            var_dump($str);die;
            $add = M('admin', '', 'KEFU')->add($str);
            if ($add) {
                $str=array('aid'=>$_SESSION['id'],'content'=>'新增账号'.$add,'ctime'=>time());
                M('admin_log','','KEFU')->add($str);
                $this->ajax('1001', '添加成功');
            } else {
                $this->ajax('1002', '添加失败');
            }
        }
    }

    public function kefuList(){
        $this->roles = role(I('rid'));
        $this->rid=I('rid');
        $this->page=I('page');
        $kefu = M('admin', '', 'KEFU');
        $search = I('get.kefu');
        if (!empty($search)) {
            $where = [];
            $yes=strstr('正常',$search);
            $no=strstr('禁用',$search);
            if($no!=''){
                $where['a.status'] = array('eq','2');
             }
            else if(!empty($yes)){
                $where['a.status'] = array('eq','1');
            }  else{
                $where['a.name|a.id'] = array('like', '%' . $search . '%');
            }
        }
        $tol = $kefu->where($where)->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field='a.*,b.name as role_name';
        $list = $kefu->alias('a')->join('role b on b.id=a.role_id')->field($field)->where($where)->order('id desc')->LIMIT($page->listfirst, $page->listRows)->select();
        foreach ($list as $k => $v) {
            if ($v['status'] == '1') {
                $list[$k]['status'] = '正常';
            }
            if ($v['status'] == '2') {
                $list[$k]['status'] = '禁用';
            }
            $list[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
        }

        $this->assign(array(
            'list' => $list,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->kefu=$search;
        $this->display();
    }

    public function kefuEdit($id,$name){
        $data = M('admin', '', 'KEFU')->where(['id' => $id])->find();
        $has_solve=M('user_message', '', 'KEFU')->where(['vid'=>'0','admin_name'=>$name])->group('uf_id')->select();
        $resolved=0;
        foreach ($has_solve as $k=>$v){
             $status=M('user_feedback', '', 'KEFU')->where(['id'=>$v['uf_id']])->getfield('status');
             if($status==3){
                 $resolved++;
             }
        }
        $data['ctime']=date('Y-m-d H:i',$data['ctime']);
        $unresolve=M('user_feedback', '', 'KEFU')->where(['status'=>'1'])->count();
        $this->assign(array(
            'id' => $id,
            'data' => $data,
            'has_back'=>count($has_solve),
            'un_resolve'=>$unresolve,
            'has_resolve'=>$resolved
        ));
        $this->role=M('role', '', 'KEFU')->field('id,name')->where(['status'=>1,'id'=>['neq',3]])->select();
        $this->apart=M('apart', '', 'KEFU')->field('id,name')->where(['status'=>1])->select();
        $this->display();
    }

    public function kefuEditSave(){
        $status = I('status');
        $id = I('id');
        $name=I('name');
        $duties=I('duties');
        $apart=I('apart');
        $name=I('name');
        $data=[];
        $data['name']=$name;
        $data['status']=$status;
        $data['level']=$duties;
        $data['part_id']=$apart;
        $data['role_id']=I('role_id');
        $data = M('admin', '', 'KEFU')->where(['id' => $id])->save($data);

        if ($data) {
            $str=array('aid'=>$_SESSION['id'],'content'=>'修改账号'.$id.'的数据','ctime'=>time());
            M('admin_log','','KEFU')->add($str);
            $this->ajax('1001', '修改成功');
        } else {
            $this->ajax('1002', '修改失败');
        }
    }

    /**
     *角色列表
     */
    public function rolelist(){
        $info = M('role', '', 'KEFU');
        $list = $info->WHERE(['status' => '1','id'=>['neq',3]])->select();
        foreach ($list as $k => $v) {
            $list[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
        }
        $this->list= $list;
        $this->display();
    }
    /**
     * 修改角色权限
     */
    public function modify_power(){
        $info = M('power', '', 'KEFU');
        $rid = I('get.rid');
        if (!empty($rid)) {
            $role = M('role', '', 'KEFU')->WHERE(['id' => $rid])->FIND();
            $top = explode(',', $role['top_id']);
            $next = explode(',', $role['next_id']);
            $last = explode(',', $role['last_id']);
            $list = $info->WHERE("status=1")->ORDER("weight asc")->SELECT();
            $list = get_new_arrays($list);

            $this->assign(array(
                'list' => $list,
                'role' => $role,
                'top' => $top,
                'next' => $next,
                'last' => $last
            ));
            $this->display();
        } else {
            $this->error('系统错误');
        }
    }
    /**
     * 修改角色权限操作
     */
    public function modify_role_power(){
        if (!empty($_POST)) {
            $rid = I('rid');
            $data['name'] = I('rolename');
            $data['top_id'] = I('top');
            $data['next_id'] = I('next');
            $data['last_id'] = I('last');
            $res = M('role', '', 'KEFU')->WHERE(['id' => $rid])->SAVE($data);
            if ($res !== false) {
                $content = "修改了" . I('rolename') . '的权限';
                $str=array('aid'=>$_SESSION['id'],'content'=>$content,'ctime'=>time());
                $record=M('admin_log','','KEFU')->add($str);
                $this->ajaxReturn(['code' => '1001', 'result' => '修改成功']);
            } else {
                $this->ajaxReturn(['code' => '1002', 'result' => '修改失败']);
            }
        }
    }

    /**
     * 删除角色操作
     */
    public function del_role(){
        $info = M('role', '', 'KEFU');
        if (!empty($_POST)) {
            $rid = I('rid');
            $role = $info->WHERE(['id' => $rid])->FIND();
            $res = $info->WHERE(['id' => $rid])->setField('status', '2');
            if ($res) {
                $content = '删除了' . $role['rolename'] . '角色';
                $str=array('aid'=>$_SESSION['id'],'content'=>$content,'ctime'=>time());
                $record=M('admin_log','','KEFU')->add($str);
                $data['code'] = '1001';
                $data['result'] = '删除成功';
            } else {
                $data['code'] = '1002';
                $data['result'] = '删除失败';
            }
            $this->ajaxReturn($data);
        }
    }

    /**
     * 添加角色并分配权限页面
     */
    public function addrole()
    {
        $info = M('power', '', 'KEFU');
        $list = $info->WHERE("status=1")->ORDER("weight asc")->SELECT();
        $list = get_new_arrays($list);

        $this->assign(array(
            'list' => $list
        ));
        $this->display();
    }

    /**
     * 添加角色操作
     */
    public function add_role(){
        if (!empty($_POST)) {
            $data['name'] = I('rolename');
            $data['top_id'] = I('top');
            $data['next_id'] = I('next');
            $data['last_id'] = I('last');
            $data['ctime'] = time();
            $res = M('role', '', 'KEFU')->add($data);
            if ($res) {
                $content = "添加了" . I('rolename') . '职位';
                $str=array('aid'=>$_SESSION['id'],'content'=>$content,'ctime'=>time());
                $record=M('admin_log','','KEFU')->add($str);
                $this->ajaxReturn(['code' => '1001', 'result' => '添加成功']);
            } else {
                $this->ajaxReturn(['code' => '1002', 'result' => '添加失败']);
            }
        }
    }

    /**
     * 发送post请求
     * @param string $url 请求地址
     * @param array $post_data post键值对数据
     * @return string
     */
    public function send_post($url, $post_data)
    {

        $postdata = http_build_query($post_data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
}